//
//  VideoHeaderDetailCollectionViewCell.m
//  Project_B
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "VideoHeaderDetailCollectionViewCell.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@interface VideoHeaderDetailCollectionViewCell ()

// 大标题Label
@property (nonatomic, strong)UILabel *titleLabel;
// 副标题Label
@property (nonatomic, strong)UILabel *subtitleLabel;
// 播放器
@property (nonatomic, strong)AVPlayerViewController *moviePlayer;
// 时间长度Label
@property (nonatomic, strong)UILabel *timeLabel;
// 观看次数Label
@property (nonatomic, strong)UILabel *numberLabel;
// 播放器占位图
@property (nonatomic, strong)UIImageView *videoImageView;

@end

@implementation VideoHeaderDetailCollectionViewCell

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}




- (void)createSubViews
{
    
#pragma mark - 使底层contentView
    self.contentView.backgroundColor = [UIColor whiteColor];
    // 边框宽度
    self.contentView.layer.borderWidth = 1.5;
    // 边框颜色
    self.contentView.layer.borderColor = [UIColor colorWithWhite:0.831 alpha:1.000].CGColor;
    
    
    
#pragma mark - 装饰View
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithWhite:0.890 alpha:1.000];
    [self.contentView addSubview:view];
    // masonry
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(-40);
        make.height.mas_equalTo(1);
        
    }];
    
    
    
    
    
#pragma mark - 创建视频播放器
    self.moviePlayer = [[AVPlayerViewController alloc] init];
    [self.contentView addSubview:self.moviePlayer.view];
    // masonry
    [self.moviePlayer.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(215);
        
    }];
    
    
    
    
    
    
#pragma mark - 播放器占位图
    self.videoImageView = [[UIImageView alloc] init];
    //    self.videoImageView.backgroundColor = [UIColor greenColor];
    // 图片按照比例铺
    self.videoImageView.contentMode = UIViewContentModeScaleAspectFill;
    // 切边框
    self.videoImageView.layer.masksToBounds = YES;
    // 打开用户交互
    self.videoImageView.userInteractionEnabled = YES;
    [self.contentView addSubview:self.videoImageView];
    // masonry
    [self.videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(215);
        
    }];
    // 轻拍
    UITapGestureRecognizer *tapStartPlayer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapStartPlayerAction:)];
    [self.videoImageView addGestureRecognizer:tapStartPlayer];
    
    
    
    
#pragma mark - 开始播放按钮
    UIButton *startPlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    startPlayButton.frame = CGRectMake((SCREEN_WIDTH - 10 - 60) / 2, (215 - 60) / 2, 60, 60);
    //    startPlayButton.backgroundColor = [UIColor greenColor];
    startPlayButton.alpha = 0.7;
    [startPlayButton setImage:[UIImage imageNamed:@"videoStartButtonImage.png"] forState:UIControlStateNormal];
    [startPlayButton addTarget:self action:@selector(startPlayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 加载imageView上，点击开始播放的时候一并隐藏
    [self.videoImageView addSubview:startPlayButton];
    
    
    
    
#pragma mark - 大标题Label
    self.titleLabel = [[UILabel alloc] init];
    //    self.titleLabel.backgroundColor = [UIColor blueColor];
    self.titleLabel.font = [UIFont systemFontOfSize:16.5];
    [self.contentView addSubview:self.titleLabel];
    // masonry
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(self.moviePlayer.view.mas_bottom).offset(10);
        make.height.mas_equalTo(30);
        
    }];
    
    
    
    
#pragma mark - 副标题label
    self.subtitleLabel = [[UILabel alloc] init];
    //    self.subtitleLabel.backgroundColor = [UIColor redColor];
    self.subtitleLabel.font = [UIFont systemFontOfSize:12.5];
    self.subtitleLabel.textColor = [UIColor colorWithWhite:0.619 alpha:1.000];
    [self.contentView addSubview:self.subtitleLabel];
    // masonry
    [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(15);
        
    }];
    
    
    
    
#pragma mark - 时间长度
    // 钟表小图片
    UIImageView *timeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-shizhong-2.png"]];
    [self.contentView addSubview:timeImageView];
    // masonry
    [timeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(11);
        make.bottom.mas_equalTo(-11);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(18);
        
    }];
    
    
    // 传值Label
    self.timeLabel = [[UILabel alloc] init];
    //    self.timeLabel.backgroundColor = [UIColor redColor];
    self.timeLabel.font = [UIFont systemFontOfSize:13.5];
    self.timeLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.timeLabel];
    // masonry
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timeImageView.mas_right).mas_offset(5);
        make.bottom.mas_equalTo(-11);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(18);
    }];
    
    
    
    
#pragma mark - 观看次数
    // 视频按钮小图片
    UIImageView *numberImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-bofang.png"]];
    [self.contentView addSubview:numberImageView];
    // masonry
    [numberImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel.mas_right).mas_offset(20);
        make.bottom.mas_equalTo(-11);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(18);
        
    }];
    
    
    // 传值label
    self.numberLabel = [[UILabel alloc] init];
    //    self.numberLabel.backgroundColor = [UIColor greenColor];
    self.numberLabel.font = [UIFont systemFontOfSize:13.5];
    self.numberLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.numberLabel];
    // masonry
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(numberImageView.mas_right).mas_offset(5);
        make.bottom.mas_equalTo(-11);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(18);
        
    }];
    
    
    
#pragma mark - 1.通知：注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopPlayer) name:@"stopPlayer" object:nil];
    
}




#pragma mark - 播放器占位图轻拍开始播放视频
- (void)tapStartPlayerAction:(UITapGestureRecognizer *)sender
{
#pragma mark - 2.通知：发送一个消息通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayer" object:nil];
    // 隐藏占位图
    self.videoImageView.hidden = YES;
    // 视频开始播放
    [self.moviePlayer.player play];
    
}





#pragma mark - 开始播放按钮
- (void)startPlayButtonAction:(UIButton *)btn
{
#pragma mark - 2.通知：发送一个消息通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayer" object:nil];
    // 隐藏占位图
    self.videoImageView.hidden = YES;
    // 视频开始播放
    [self.moviePlayer.player play];
    
}




#pragma mark - 重写set方法
- (void)setHeaderDetailModel:(VideoHeaderDetailModel *)headerDetailModel
{
    _headerDetailModel = headerDetailModel;
    
    // 赋值
    self.titleLabel.text = self.headerDetailModel.title;
    self.subtitleLabel.text = self.headerDetailModel.description1;
    
    // 播放时长赋值
    NSInteger minutes = self.headerDetailModel.length / 60;
    NSInteger seconds = self.headerDetailModel.length % 60;
    NSString *timeStr = [NSString stringWithFormat:@"0%ld:%ld", minutes, seconds];
    self.timeLabel.text = timeStr;
    
    // 播放次数赋值
    CGFloat wan = self.headerDetailModel.playCount / 10000.0;
    NSString *playCountStr = [NSString stringWithFormat:@"%.1f万", wan];
    self.numberLabel.text = playCountStr;
    
    // 播放器占位图片
    [self.videoImageView sd_setImageWithURL:[NSURL URLWithString:self.headerDetailModel.cover] placeholderImage:PLACEHOLDER_IMAGE];
    
    // 视频赋值
    self.moviePlayer.player = [AVPlayer playerWithURL:[NSURL URLWithString:self.headerDetailModel.mp4_url]];
    
    
#pragma mark - 解决cell重用问题
    // 默认不隐藏
    self.videoImageView.hidden = NO;
    // 判断是否隐藏
    if (self.headerDetailModel.isSelected) {
        self.videoImageView.hidden = YES;
    }
    
}




#pragma mark - 3.通知：获得发送消息传递过来的值
- (void)stopPlayer
{
    // 显示播放器的展位图片
    self.videoImageView.hidden = NO;
    // 让视频从头开始播放
    [self.moviePlayer.player seekToTime:kCMTimeZero];
    // 让视频暂停
    [self.moviePlayer.player pause];
}


@end